local Addons = Addons

local function GetLocaleString(addon, name)
	local l = GetLocale()
	local res = addon.data[name.."-"..l]
	if not res then
		res = addon.data[name]
	end
	return res
end

local function getinfo(id)
	if type(id) == "string" and id ~= "FrameXML" then
		return Addons.GetAddOnInfo(id)
	elseif type(id) == "number" and id > 0 then
		return Addons.GetAddOnInfo(id + Addons.hidden_count)
	end
end

function DisableAddOn(id)
	local addon = getinfo(id)
	if addon then
		Addons.disabled[addon.name] = true
	end
end

function DisableAllAddOns()
	for _, addon in ipairs(addons) do
		Addons.disabled[addon.name] = true
	end
end

function EnableAddOn(id)
	local addon = getinfo(id)
	if addon then
		Addons.disabled[addon.name] = nil
	end
end

function EnableAllAddOns()
	for _, addon in ipairs(addons) do
		Addons.disabled[addon.name] = nil
	end
end

function GetAddOnDependencies(id)
	local addon = getinfo(id)
	if addon then
		local deps = addon.deps
		if not deps then
			local temp = (" ,"):split(addon.data["Dependencies"])
			deps = {}
			for _, v in ipairs(temp) do
				if v ~= "" then
					deps[#deps + 1] = v
				end
			end
			addon.deps = deps
		end
		return deps
	end
end

function GetAddOnInfo(id)
	local addon = getinfo(id)
	if addon then
		return addon.name,
			GetLocaleString(addon, "Title"),
			GetLocaleString(addon, "Notes"),
			not Addons.disabled[addon.name],
			addon.loadable,
			addon.reason,
			addon.security
	else
		return id, nil, nil, nil, nil, "UNKNOWN"
	end
end

function GetAddOnMetadata(id, variable)
	local addon = getinfo(id)
	if addon then
		return GetLocaleString(addon, variable)
	end
end

function GetNumAddOns()
	return #Addons.addons - Addons.hidden_count
end

function IsAddOnLoaded(id)
	local addon = getinfo(id)
	if addon then
		return Addons.loaded[addon.name]
	end
end

function IsAddOnLoadOnDemand(id)
	local addon = getinfo(id)
	if addon then
		return addon.data["LoadOnDemand"] == "1"
	end
end

function LoadAddOn(name)
	return Addons.Load(true, name)
end

function ResetDisabledAddOns()
	--[[ @fixme:
		This probably reset the list of disabled addons.
		This list is saved between session by WoW.
	]]
	Addons.disabled = {}
end
